﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace uTorrentExtractor
{
    [Serializable]
    public class TorrentLog : ISerializable
    {
        private List<AccessLog> _accessLogs;
        private bool _unpacked;
        private DateTime? _dateUnpacked;
        private bool _cleaned;
        private DateTime? _dateCleaned;
        private List<ErrorLog> _errorLogs;

        public TorrentLog()
        {
            AccessLogs = new List<AccessLog>();
            ErrorLogs = new List<ErrorLog>();
            Unpacked = false;
            Cleaned = false;
        }

        public TorrentLog(SerializationInfo info, StreamingContext ctxt)
        {
            this.AccessLogs = (List<AccessLog>)info.GetValue("AccessLogs", typeof(List<AccessLog>));
            this.ErrorLogs = (List<ErrorLog>)info.GetValue("ErrorLogs", typeof(List<ErrorLog>));
            this.Unpacked = (bool)info.GetValue("Unpacked", typeof(bool));
            this.DateUnpacked = (DateTime?)info.GetValue("DateUnpacked", typeof(DateTime?));
            this.Cleaned = (bool)info.GetValue("Cleaned", typeof(bool));
            this.DateCleaned = (DateTime?)info.GetValue("DateCleaned", typeof(DateTime?));
        }

        public List<AccessLog> AccessLogs
        {
            get { return _accessLogs; }
            set { _accessLogs = value; }
        }

        public List<ErrorLog> ErrorLogs
        {
            get { return _errorLogs; }
            set { _errorLogs = value; }
        }

        public bool Unpacked
        {
            get { return _unpacked; }
            set { _unpacked = value; }
        }

        public bool Cleaned
        {
            get { return _cleaned; }
            set { _cleaned = value; }
        }

        public DateTime? DateUnpacked
        {
            get { return _dateUnpacked; }
            set { _dateUnpacked = value; }
        }

        public DateTime? DateCleaned
        {
            get { return _dateCleaned; }
            set { _dateCleaned = value; }
        }


        public bool HasOpenAccessLog
        {
            get
            {
                return this.AccessLogs.Any(al => al.End == null);
            }
        }

        public void CloseAllAccessLogs()
        {
            foreach (var accesslog in this.AccessLogs.Where(al => al.End == null))
            {
                accesslog.End = DateTime.Now;
            }
        }

        public void CreateNewAccessLog(string description)
        {
            AccessLogs.Add(new AccessLog(DateTime.Now, description));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("ErrorLogs", ErrorLogs);
            info.AddValue("AccessLogs", AccessLogs);
            info.AddValue("Unpacked", Unpacked);
            info.AddValue("DateUnpacked", DateUnpacked);
            info.AddValue("Cleaned", Cleaned);
            info.AddValue("DateCleaned", DateCleaned);
        }
    }

}
